"""
数据获取网址对象
    根据不同的数据构造不同的网址
"""
from QFinanceGridModel.base import generate_ut_token


class Url:
    def __init__(self, type, secid=None):
        """
        :param type:返回的url类型,2(行业),3(概念),4(个股榜),5(个股)
        :param secid:如果type为4或5时不能为空,其它情况为空
        """

        self.__type = type
        self.__secid = secid

    @property
    def base_url(self):
        if self.__type == "2" or self.__type == "3":
            return self.__generate_block_data_url(self.__type)[0]
        if self.__type == "4" and self.__secid != None:
            return self.__generate_single_data_url(self.__secid)[0]
        if self.__type == "5" and self.__secid != None:
            return self.__generate_realtime_data_url(self.__secid)[0]

    @property
    def params(self):
        if self.__type == "2" or self.__type == "3":
            return self.__generate_block_data_url(self.__type)[1]
        if self.__type == "4" and self.__secid != None:
            return self.__generate_single_data_url(self.__secid)[1]
        if self.__type == "5" and self.__secid != None:
            return self.__generate_realtime_data_url(self.__secid)[1]

    def __generate_realtime_data_url(self, secid):  # 个股分时图url
        base_url = "https://push2.eastmoney.com/api/qt/stock/details/sse"
        params = {
            "fields1": "f1,f2,f3,f4",
            "fields2": "f51,f52,f53,f54,f55",
            "mpi": "2000",
            "ut": generate_ut_token(),
            "fltt": "2",
            "pos": "-0",
            "wbp2u": "|0|0|0|web",
            "secid": secid
        }
        return base_url, params

    def __generate_block_data_url(self, secid):  # 行业/概念

        fs = "m:90 t:2" if secid == "2" else "m:90 t:3"

        base_url = "https://push2.eastmoney.com/api/qt/clist/get"
        params = {
            "fid": "f62",
            "po": "1",
            "pz": "100",
            "pn": "1",
            "np": "1",
            "fltt": "2",
            "invt": "2",
            "ut": generate_ut_token(),
            "fs": fs,
            "fields": "f12,f14,f2,f3,f62,f184,f66,f69,f72,f75,f78,f81,f84,f87,f204,f205,f124,f1,f13"
        }
        return base_url, params

    def __generate_single_data_url(self, secid):  # 行业/概念细节
        fs = fs = f"b:{secid}"

        base_url = "https://push2.eastmoney.com/api/qt/clist/get"
        params = {
            "fid": "f62",
            "po": "1",
            "pz": "100",
            "pn": "1",
            "np": "1",
            "fltt": "2",
            "invt": "2",
            "ut": generate_ut_token(),
            "fs": fs,
            "fields": "f12,f14,f2,f3,f62,f184,f66,f69,f72,f75,f78,f81,f84,f87,f204,f205,f124,f1,f13"
        }
        return base_url, params
